package com.platform.wechat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.platform.wechat.common.Constant;
import com.platform.wechat.common.WechatAPI;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author CX
 * @create 2021-07-28 10:04
 */

/**
 * 114微信公众号菜单
 */
public class Wechat114Menu {
    public static void main(String[] args) {
        //设置post请求
        HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+Constant.getAccessToken());
        //创建客户端
        HttpClient httpClient = new DefaultHttpClient();
        //封装传入数据
        JSONObject bigJson = new JSONObject();
        //一级菜单Json集合
        JSONArray buttonArray = new JSONArray();
        //第一个一级菜单 1
        JSONObject firstMenuJson = new JSONObject();
        firstMenuJson.put("name","核酸检测");
        firstMenuJson.put("type","view");
        firstMenuJson.put("url",Constant.oAuth(WechatAPI.URL + "/hsjc.html", Constant.SNSAPI_USERINFO));
        //第二个一级菜单 2
        JSONObject secondMenuJson = new JSONObject();
        secondMenuJson.put("name","就医服务");
        secondMenuJson.put("type","click");
        secondMenuJson.put("key","");
        //第三个一级菜单 3
        JSONObject thirdMenuJson = new JSONObject();
        thirdMenuJson.put("name","关于我们");
        thirdMenuJson.put("type","click");
        thirdMenuJson.put("key","");
        /*----------------------------------------------------------------------------------------*/

        //第二个一级菜单的二级菜单集合
        JSONArray secondsubArray = new JSONArray();

        //第二个一级菜单的第一个二级菜单 2-1
        JSONObject secondsub1 = new JSONObject();
        secondsub1.put("type","view");
        secondsub1.put("name","预约挂号");
        secondsub1.put("url",Constant.oAuth(WechatAPI.URL + "/index.html", Constant.SNSAPI_USERINFO));


        //第二个一级菜单的第二个二级菜单 2-2
        JSONObject secondsub2 = new JSONObject();
        secondsub2.put("type","view");
        secondsub2.put("name","个人中心");
        secondsub2.put("url",Constant.oAuth(WechatAPI.URL + "/center.html", Constant.SNSAPI_USERINFO));

        secondsubArray.add(secondsub1);
        secondsubArray.add(secondsub2);
        secondMenuJson.put("sub_button",secondsubArray);

        /*-------------------------------------------------------------------------------------------------*/
        //第三个一级菜单的二级菜单集合
        JSONArray thirdsub1Array = new JSONArray();

        //第三个一级菜单的第一个二级菜单 3-1
        JSONObject thirdsub1 = new JSONObject();
        thirdsub1.put("type","view");
        thirdsub1.put("name","账号申诉");
        thirdsub1.put("url","");

        //第三个一级菜单的第二个二级菜单 3-2
        JSONObject thirdsub2 = new JSONObject();
        thirdsub2.put("type","view");
        thirdsub2.put("name","合作洽谈");
        thirdsub2.put("url","");

        //第三个一级菜单的第一个二级菜单 3-3
        JSONObject thirdsub3 = new JSONObject();
        thirdsub3.put("type","view");
        thirdsub3.put("name","反馈建议");
        thirdsub3.put("url","");

        thirdsub1Array.add(thirdsub1);
        thirdsub1Array.add(thirdsub2);
        thirdsub1Array.add(thirdsub3);
        thirdMenuJson.put("sub_button",thirdsub1Array);

        /*-------------------------------------------------------------------------------------------------*/
        //封装
        buttonArray.add(firstMenuJson);
        buttonArray.add(secondMenuJson);
        buttonArray.add(thirdMenuJson);
        bigJson.put("button",buttonArray);
        //将数据放入post请求体中
        httpPost.setEntity(new StringEntity(bigJson.toString(),"utf-8"));
        //执行发送请求
        try {
            //获取响应结果
            HttpResponse httpResponse = httpClient.execute(httpPost);
            //判断请求是否成功
            if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                String string = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
                System.out.println(string);
                JSONObject jsonObject = JSON.parseObject(string);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
